package com.tarena.lbs.content.controller;

import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.model.Result;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.content.pojo.param.ArticleActionParam;
import com.tarena.lbs.content.pojo.param.ArticleContentParam;
import com.tarena.lbs.content.pojo.query.ArticleQuery;
import com.tarena.lbs.content.pojo.vo.ArticleActionPageResultVO;
import com.tarena.lbs.content.pojo.vo.ArticleVO;
import com.tarena.lbs.content.service.ArticleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.constraints.NotBlank;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Set;

/**
 * 文章业务模块接口
 */
@Slf4j
@RestController
@Tag(name = "文章相关功能接口")
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    @Operation(summary = "文章分页列表查询")
    @PostMapping("/admin/content/article/list")
    public Result<PageResult<ArticleVO>> pageList( ArticleQuery query) throws BusinessException {
        PageResult<ArticleVO> page = articleService.pageList(query);
        return null;
    }

    @Operation(summary = "文章保存")
    @PostMapping("/admin/content/article/insert")
    public Result<Void> addArticle(ArticleContentParam param) throws BusinessException {
        log.debug("文章保存,参数={}", param);
        articleService.addArticle(param);
        return Result.success();
    }
    @Operation(summary = "文章更新")
    @PostMapping("/admin/content/article/update")
    public Result<Void>updateArticle(ArticleContentParam param)
            throws BusinessException, IllegalAccessException {
        articleService.updateArticle(param);
        return Result.success();
    }
    @Operation(summary = "文章删除")
    @PostMapping("/admin/content/article/delete/id")
    public Result<Void> deleteArticle(@PathVariable String id)
            throws BusinessException, IllegalAccessException {
        articleService.deleteArticle(id);
        return Result.success();
    }


    @Operation(summary = "文章标签集合")
    @GetMapping("/admin/content/article/getArticleLabel")
    public Result<Set<String>> articleLabels(@Validated ArticleQuery query) throws BusinessException {
        log.debug("文章标签集合, 参数={}", query);
Set<String>labels=articleService.articleabels(query);
        return new Result<>(null);
    }


    @Operation(summary = "文章详情")
    @GetMapping("/admin/content/article/get")
    @Parameter(name = "id", description = "文章Id", required = true, example = "1")
    public Result<ArticleVO> articleDetail(@NotBlank(message = "文章Id不能为空") String id) throws BusinessException, IllegalAccessException {
        log.debug("文章详情, 参数={}", id);
        ArticleVO article=articleService.articleDetail(id);
        return new Result<>(article);
    }


    @Operation(summary = "用户行为记录")
    @PostMapping("/admin/content/article/behavior")
    public Result<Void> articleBehavior(ArticleActionParam param)
        throws BusinessException{
        log.debug("用户行为 参数={}",param);
        ArticleQuery articleQuery = new ArticleQuery();
        BeanUtils.copyProperties(param,articleQuery);
        articleService.getBehaviorLists(articleQuery);
        return Result.success();
    }


    @Operation(summary = "查询已经点赞 收藏 访问过的文章")
    @GetMapping("admin/content/article/getBehaviorList")
    public Result<ArticleActionPageResultVO> getBehaviorLists( ArticleQuery query)
        throws BusinessException{
        log.debug("查询已经点赞，收藏 访问过的文章, 参数={}",query);
        // TODO: 实现查询用户行为列表逻辑

        return articleService.getBehaviorLists(query);
    }
}